package com.bookquery.servlet;

import com.alibaba.fastjson.JSON;
import com.bookquery.entity.BookQuery;
import com.bookquery.service.BookQueryService;
import com.bookquery.service.impl.BookQueryServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@WebServlet(urlPatterns = {"/query"}, name = "ShowBookListServlet")
public class ShowBookListServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        doGet(request, response);
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        BookQueryService bookQueryService = new BookQueryServiceImpl();
        PrintWriter out = response.getWriter();
        //默认加载页面查询所有方法
        String bookName = request.getParameter("bookName");
        String bookAuthor = request.getParameter("bookAuthor");
        String bookType = request.getParameter("bookType");
        if (bookType == "0") {
            bookType = null;
        }
        List<BookQuery> bookQueryList = bookQueryService.queryBook(bookName, bookAuthor, bookType);
        String bookJson = JSON.toJSONString(bookQueryList);
        out.print(bookJson);
        out.close();
        out.flush();
    }

}
